package cn.hahah.module.blog.service.userhistorysearch;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userhistorysearch.vo.*;
import cn.hahah.module.blog.dal.dataobject.userhistorysearch.UserHistorySearchDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userhistorysearch.UserHistorySearchMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户搜索记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserHistorySearchServiceImpl implements UserHistorySearchService {

    @Resource
    private UserHistorySearchMapper userHistorySearchMapper;

    @Override
    public Long createUserHistorySearch(UserHistorySearchSaveReqVO createReqVO) {
        // 插入
        UserHistorySearchDO userHistorySearch = BeanUtils.toBean(createReqVO, UserHistorySearchDO.class);
        userHistorySearchMapper.insert(userHistorySearch);
        // 返回
        return userHistorySearch.getId();
    }

    @Override
    public void updateUserHistorySearch(UserHistorySearchSaveReqVO updateReqVO) {
        // 校验存在
        validateUserHistorySearchExists(updateReqVO.getId());
        // 更新
        UserHistorySearchDO updateObj = BeanUtils.toBean(updateReqVO, UserHistorySearchDO.class);
        userHistorySearchMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserHistorySearch(Long id) {
        // 校验存在
        validateUserHistorySearchExists(id);
        // 删除
        userHistorySearchMapper.deleteById(id);
    }

    private void validateUserHistorySearchExists(Long id) {
        if (userHistorySearchMapper.selectById(id) == null) {
            throw exception(USER_HISTORY_SEARCH_NOT_EXISTS);
        }
    }

    @Override
    public UserHistorySearchDO getUserHistorySearch(Long id) {
        return userHistorySearchMapper.selectById(id);
    }

    @Override
    public PageResult<UserHistorySearchDO> getUserHistorySearchPage(UserHistorySearchPageReqVO pageReqVO) {
        return userHistorySearchMapper.selectPage(pageReqVO);
    }

}